package com.gopay.scheduler.common;

import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.remoting.RemoteAccessException;
import org.springframework.stereotype.Service;

import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.cps.vo.CPSRequest;
import com.gopay.common.domain.cps.vo.CPSResponse;
import com.gopay.common.domain.cps.vo.CloudCPSRequest;
import com.gopay.common.domain.cps.vo.CloudCPSResponse;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.cps.dispatch.CPSCallRemoteService;
import com.gopay.remote.cps.dispatch.CloudCPSCallRemoteService;

/**
 * 访问CPS辅助类
 * 
 * @CallCPSUtil.java
 * @author fanghw
 * @2013-2-16 上午11:38:46 www.gopay.com.cn Inc.All rights reserved.
 */
@Service(value = "callCPSHelper")
public class CallCPSHelper {    
    protected final Logger logger = Logger.getLogger(this.getClass());
    
    @Resource(name = "callCPSClient")
    private CPSCallRemoteService callCPSClient;
    
    /**
     * 调用跨境的cps
     */
    @Resource(name = "callCbRmbCPSClient")
    private CPSCallRemoteService callCbRmbCPSClient;
    /**
     * E商通
     */
    @Resource(name = "callCloudCPSClient")
    private CloudCPSCallRemoteService callCloudCPSClient;
    
    
   
    /**
     * 根据VO Ticket方式调用CPS
     * 
     * @param v
     * @return
     * @throws MalformedURLException
     */
    public  CPSResponse callByVOTicket(Object v) throws GopayException {
        // v为VO实例
        return callCPS("0", "001", v);
    }
        
    
    public  CPSResponse callCbRmbByVOTicket(Object v) throws GopayException {
        // v为VO实例
        return callCbRmbCPS("0", "001", v);
    }
    
    public  CloudCPSResponse callCloudByVOTicket(Object v) throws GopayException {
        // v为VO实例
        return callCloudCPS("0", "001", v);
    }
    /**
     * stv三个参数方式调用CPS
     * 
     * @param s
     * @param t
     * @param v
     * @return
     * @throws MalformedURLException
     */
    private  CPSResponse callCPS(String s, String t, Object v) throws GopayException {
        CPSResponse response = null;

        try {
            // 封装调用cps服务的请求baggage
            Map<String, Object> baggage = new HashMap<String, Object>();
            baggage.put("s", s);
            baggage.put("t", t);
            baggage.put("v", v);

            // 封装cps请求对象
            CPSRequest request = new CPSRequest();
            request.setSequence(System.currentTimeMillis());
            request.setReqData(baggage);

            // 调用cps服务
             response = callCPSClient.callCPS(request);

            // 校验调用序列值是否相同
            if (response.getSequence() != request.getSequence()) {
                logger.error("CallCPSHelper---request.getSequence()");
                throw new GopayException(ProcCodeConstants.PROC_CODE_100F3001);
            }

        }catch (RemoteAccessException e) {
            logger.error("CallCPSHelper---callCPS异常，描述="+e.getMessage());
            throw new GopayException(ProcCodeConstants.PROC_CODE_100F3001);
        } catch (Exception e) {
            logger.error("CallCPSHelper---callCPS", e);
            throw new GopayException(ProcCodeConstants.PROC_CODE_100F3001);
        }
        return response;
    }
    /**
     * stv三个参数方式调用CPS
     * 
     * @param s
     * @param t
     * @param v
     * @return
     * @throws MalformedURLException
     */
    private  CloudCPSResponse callCloudCPS(String s, String t, Object v) throws GopayException {
    	CloudCPSResponse response = null;

        try {
            // 封装调用cps服务的请求baggage
            Map<String, Object> baggage = new HashMap<String, Object>();
            baggage.put("s", s);
            baggage.put("t", t);
            baggage.put("v", v);

            // 封装cps请求对象
            CloudCPSRequest request = new CloudCPSRequest();
            request.setSequence(System.currentTimeMillis());
            request.setReqData(baggage);

            // 调用cps服务
             response = callCloudCPSClient.callCloudCPS(request);

            // 校验调用序列值是否相同
            if (response.getSequence() != request.getSequence()) {
                logger.error("CallCPSHelper---request.getSequence()");
                throw new GopayException(ProcCodeConstants.PROC_CODE_100F3001);
            }

        }catch (RemoteAccessException e) {
            logger.error("CallCPSHelper---callCPS异常，描述="+e.getMessage());
            throw new GopayException(ProcCodeConstants.PROC_CODE_100F3001);
        } catch (Exception e) {
            logger.error("CallCPSHelper---callCPS", e);
            throw new GopayException(ProcCodeConstants.PROC_CODE_100F3001);
        }
        return response;
    }
    /**
     * stv三个参数方式调用CPS
     * 
     * @param s
     * @param t
     * @param v
     * @return
     * @throws MalformedURLException
     */
    private  CPSResponse callCbRmbCPS(String s, String t, Object v) throws GopayException {
        CPSResponse response = null;

        try {
            // 封装调用cps服务的请求baggage
            Map<String, Object> baggage = new HashMap<String, Object>();
            baggage.put("s", s);
            baggage.put("t", t);
            baggage.put("v", v);

            // 封装cps请求对象
            CPSRequest request = new CPSRequest();
            request.setSequence(System.currentTimeMillis());
            request.setReqData(baggage);

            // 调用cps服务
             response = callCbRmbCPSClient.callCPS(request);

            // 校验调用序列值是否相同
            if (response.getSequence() != request.getSequence()) {
                logger.error("CallCPSHelper---request.getSequence()");
                throw new GopayException(ProcCodeConstants.PROC_CODE_100F3001);
            }

        }catch (RemoteAccessException e) {
            logger.error("CallCPSHelper---callCPS异常，描述="+e.getMessage());
            throw new GopayException(ProcCodeConstants.PROC_CODE_100F3001);
        } catch (Exception e) {
            logger.error("CallCPSHelper---callCPS", e);
            throw new GopayException(ProcCodeConstants.PROC_CODE_100F3001);
        }
        return response;
    }
    
    public CPSCallRemoteService getCallCPSClient() {
        return callCPSClient;
    }

    public void setCallCPSClient(CPSCallRemoteService callCPSClient) {
        this.callCPSClient = callCPSClient;
    }

	public CPSCallRemoteService getCallCbRmbCPSClient() {
		return callCbRmbCPSClient;
	}

	public void setCallCbRmbCPSClient(CPSCallRemoteService callCbRmbCPSClient) {
		this.callCbRmbCPSClient = callCbRmbCPSClient;
	}
    
    
}
